package com.zxw.mallafter.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zxw.mallafter.dao.UserDao;
import com.zxw.mallafter.model.AdminExtend;
import com.zxw.mallafter.service.UserService;
import model.UserExtend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import util.PageInfo;

import java.util.List;
import java.util.Objects;

@Service
public class UserServiceImpl implements UserService {
    private UserDao userDao;
    @Autowired
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }


    @Override
    public List<UserExtend> getall(PageInfo pageInfo, UserExtend userExtend) {
        Page<?> __= PageHelper.startPage(pageInfo.getPageNo(),pageInfo.getPageSize());
        try{
            return userDao.getall(userExtend);
        }catch(Exception e){
            return null;
        }
        }

    @Override
    public int delete(Integer[] ids) {
      try{
          return userDao.delete(ids);
      }catch(Exception e){
          return 0;
      }
    }

    @Override
    public int insert(UserExtend userExtend) {
        //判断userId userName sex password birthday 是否为空

        if ( userExtend.getUserId().isEmpty() || userExtend.getUserName().isEmpty() ||userExtend.getSex()==null||
                userExtend.getSex().isEmpty() || userExtend.getPassword().isEmpty() ||userExtend.getBirthday()==null) {
            return 0;

        }
        try{
            return userDao.insert(userExtend);
        }catch(Exception e){
            return 0;
        }
    }

    @Override
    public int update(UserExtend userExtend) {
        //判断userId userName sex password birthday 是否为空
        if ( userExtend.getUserId().isEmpty() || userExtend.getUserName().isEmpty() ||
                userExtend.getSex().isEmpty() || userExtend.getPassword().isEmpty()||userExtend.getBirthday()==null) {
            return 0;

        }
        try{
            return userDao.update(userExtend);
        }catch(Exception e){
            return 0;
        }
    }

    @Override
    public int updateStatus(Integer id, String status) {
        if(Objects.equals(status, "1")){
            status="0";
        }else {
            status="1";
        }
        try{
            return userDao.updateStatus(id,status);
        }catch(Exception e){
            return 0;
        }
    }
}



